home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / COMMON.ZIP / PR2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-14  |  5.2 KB  |  290 lines

  1. /*
  2.  
  3.  
  4.     pr2: copyright 1992, Alec Russell, all rights reserved
  5.  
  6.     Author: Alec Russell
  7.  
  8.     direct MDA output routines
  9.  
  10.     usefull for debugging
  11.  
  12.  
  13.    Created: March 2, 1992
  14.  
  15.  
  16.    Tested in Medium mem model (near data, far code)
  17.  
  18.    Borland C++ 3.1 used.
  19.    _f....() are far only versions of the functions
  20.  
  21. */
  22.  
  23.  
  24.  
  25. #include <stdio.h>
  26. #include <dos.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include <conio.h>
  30.  
  31.  
  32.  
  33. /* set to zero to skip the main() so you can link with other stuff */
  34. #define TEST 1
  35.  
  36. #pragma inline
  37.  
  38. unsigned int mda_segment=0xB000;  /* mda */
  39. /* unsigned int mda_segment=0xB800; */   /* cga/ega/vga */
  40.  
  41. /* to use the CGA/EGA/VGA text screen, change
  42.   mda_segment to 0xB800
  43.  
  44. */
  45.  
  46.  
  47. /* a C version of pr2_low that is memory model independent */
  48. /* ---------------------- pr2_lowc() ------------------- January 27,1994 */
  49. void pr2_lowc(unsigned char attr, int x, int y, char *s)
  50. {
  51.    int off;
  52.    unsigned char far *mda_p;
  53.  
  54.    if ( *s )
  55.       {
  56.       off=160*y + (x<<1);
  57.       mda_p=MK_FP(mda_segment, off);
  58.       while ( *s )
  59.          {
  60.          *mda_p++=*s++;
  61.          *mda_p++=attr;
  62.          }
  63.  
  64.       }
  65.  
  66. }
  67.  
  68.  
  69. /* This code ASSUMES s is a NEAR pointer !!! */
  70. /* for a far s use     lds di, s,
  71.    del mov di, s
  72.    and pop and push ds
  73. */
  74. /* attr:text */
  75. /* ----------------------- pr2_low() ---------------------- March 11,1991 */
  76. void pr2_low(unsigned char attr, int x, int y, char *s)
  77. {
  78.    int len, off;
  79.  
  80.    len=strlen(s);
  81.    if ( len )
  82.       {
  83.       off=160*y + (x<<1);
  84.  
  85.       asm      {
  86.                   push di
  87.                   push si
  88.  
  89.                   mov ah, attr
  90.                   mov di, s
  91.                   mov es, mda_segment
  92.                   mov si, off
  93.                   mov cx, len
  94.                   }
  95.          start:
  96.       asm      {
  97.                   mov al, [di]
  98.                   mov es:si, ax
  99.                   inc di
  100.                   inc si
  101.                   inc si
  102.  
  103.                   loop start
  104.  
  105.                   pop si
  106.                   pop di
  107.          }
  108.       }
  109. }
  110.  
  111.  
  112. int gb_pr2_y=0;
  113.  
  114. /* ---------------------- pr2() ----------------------- February 29,1992 */
  115. void pr2(char *s, ...)
  116. {
  117. static char out[256];
  118.     va_list arg_ptr;
  119.  
  120.     va_start(arg_ptr, s);
  121.     vsprintf(out, s, arg_ptr);
  122.     va_end(arg_ptr);
  123.  
  124.  
  125.     pr2_low(0x03, 0, gb_pr2_y, out);
  126.     ++gb_pr2_y;
  127.     if ( gb_pr2_y > 24 )
  128.         {
  129.         /* scroll screen */
  130.         asm {
  131.             push ds
  132.  
  133.             mov es, mda_segment
  134.             mov di, 0
  135.  
  136.             mov ds, mda_segment
  137.             mov si, 160
  138.             cld
  139.             }
  140.  
  141.         start:
  142.         asm {
  143.             mov cx, 3840
  144.             rep movsb
  145.  
  146.             mov ax, 0
  147.             mov cx, 160
  148.             rep stosb
  149.  
  150.             pop ds
  151.             }
  152.  
  153.         gb_pr2_y=24;
  154.         }
  155.  
  156. }
  157.  
  158. /* ---------------------- clear_pr2() ----------------- February 29,1992 */
  159. void clear_pr2(void)
  160. {
  161.  
  162.     /* init mda ?? */
  163.     /* outportb(0x3b8, 8);   this disables the blinking */
  164.  
  165.     gb_pr2_y=0;
  166.  
  167.     /* clear mda */
  168.     asm {
  169.         push ds
  170.  
  171.         mov bx, 25
  172.         mov si, 0
  173.  
  174.         mov di, 0
  175.         mov es, mda_segment
  176.         mov ax, 0
  177.         cld
  178.         }
  179.  
  180.     start:
  181.     asm {
  182.         mov cx, 160
  183.         rep stosb
  184.  
  185.         add di, si
  186.         dec bx
  187.         jnz start
  188.  
  189.         pop ds
  190.         }
  191. }
  192.  
  193.  
  194. /* ---------------------- clear_pr2c() ----------------- January 14,1995 */
  195. void clear_pr2c(void)
  196. {
  197.    unsigned char far *mda_p;
  198.  
  199.  
  200.    /* outportb(0x3b8, 8);   this disables the blinking  */
  201.  
  202.    gb_pr2_y=0;
  203.    mda_p=MK_FP(mda_segment, 0);
  204.  
  205.    _fmemset(mda_p, 0, 80*2*25); /* 80 chars, 2 bytes per char, 25 lines */
  206.  
  207. }
  208.  
  209.  
  210.  
  211. /* ---------------------- pr2c() ----------------------- February 29,1992 */
  212. void pr2c(char *s, ...)
  213. {
  214.    static char out[256];
  215.    unsigned char far *mda_p1, far *mda_p2;
  216.    va_list arg_ptr;
  217.  
  218.     va_start(arg_ptr, s);
  219.     vsprintf(out, s, arg_ptr);
  220.     va_end(arg_ptr);
  221.  
  222.  
  223.     pr2_lowc(0x03, 0, gb_pr2_y, out);
  224.     ++gb_pr2_y;
  225.     if ( gb_pr2_y > 24 )
  226.         {
  227.         /* scroll screen */
  228.          mda_p1=MK_FP(mda_segment, 0);
  229.          mda_p2=MK_FP(mda_segment, 160);
  230.          _fmemmove(mda_p1, mda_p2, 3840); /* 2*80*24 */
  231.  
  232.          /* clear last line */
  233.          mda_p1=MK_FP(mda_segment, 3840);
  234.          _fmemset(mda_p1, 0, 160);
  235.         gb_pr2_y=24;
  236.         }
  237.  
  238. }
  239.  
  240.  
  241. #if TEST
  242.  
  243. /* for testing */
  244.  
  245. /* ---------------------- main() -------------------------- March 1,1992 */
  246. void main(void)
  247. {
  248.    int i;
  249.    char temp[90];
  250.  
  251.  
  252.    printf("Testing clearc and lowc, press a key\n");
  253.  
  254.    clear_pr2c();
  255.  
  256.    getch();
  257.  
  258.    for ( i=1; i < 20; i++ )
  259.       {
  260.       sprintf(temp, ">line %d ---<", i);
  261.       puts(temp);
  262.       pr2_lowc(3, 1, i, temp);
  263.       }
  264.  
  265.    puts("Press a key");
  266.    getch();
  267.  
  268.    printf("The asm versions, press a key\n");
  269.    clear_pr2();
  270.    getch();
  271.  
  272.    for ( i=0; i < 70; i++ )
  273.       {
  274.       sprintf(temp, ">line %d ---<", i);
  275.       puts(temp);
  276.       pr2(temp);
  277.       if ( i == 20 )
  278.          getch();
  279.       }
  280.  
  281.    puts("Press a key");
  282.    getch();
  283. }
  284.  
  285. #endif
  286.  
  287.  
  288.  
  289. /* ------------------- end of file ----------------------------- */
  290.